package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sector.Sector;
import sector.SectorHome;
import admin.Admin;
import admin.AdminHome;
import department.Department;
import department.DepartmentHome;

@SuppressWarnings("serial")
public class TestServlet extends HttpServlet{
	
	
	private PrintWriter out;
	
	
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException
	{
		doPostAndGet(req,res);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse res)
	throws IOException, ServletException
	{
		doPostAndGet(req,res);
	}
	
	protected void doPostAndGet(HttpServletRequest req ,HttpServletResponse res)
	throws IOException, ServletException
	{
		out = res.getWriter();
		printPlain("<html><head><title>TEST</title></head>\n<body>");
		String homeName = "";
		String beanType = "";
		try {
			
			
			InitialContext initialContext = new InitialContext();
			printTrace("Test of entity beans for the worplacement manager");
			
//			-- Test of Career  Bean
//			beanType = "Career";
//			homeName = "MyCareer_L";
//			printTrace("********** Test of "+beanType+" bean **********");
//			printTrace("--- Access to "+homeName+" : "+beanType+"Home");
//			CareerHome careerHome = (CareerHome)initialContext.lookup(homeName);
//			printTrace("--- Creation of a bean "+beanType);
//			Career careerOfficer = careerHome.create("Sarah","McCain","CareerOffice1","blabla","shara.maccain@ucc.com");
//			printTrace("--- Bean Career created with id: "+careerOfficer.getId());
//			printTrace("--- Bean Career created with login: "+careerOfficer.getLogin());
//			printTrace("--- Bean Career created with Surname: "+careerOfficer.getSurname());
//			printTrace("--- Bean Career created with Firstname: "+careerOfficer.getFirstname());
//			printTrace("--- Bean Career created with Email: "+careerOfficer.getEmail());
//			printTrace("--- Bean Career created with password: "+careerOfficer.getPassword());
//			printTrace("-----------------------------");
//			careerOfficer.setEmail("shara1.maccain@ucc.com");
//			careerOfficer.setFirstname("Sara");
//			careerOfficer.setLogin("SarahM");
//			careerOfficer.setPassword("blabla1");
//			careerOfficer.setSurname("Cain");
//			printTrace("--- Bean Career modified with id: "+careerOfficer.getId());
//			printTrace("--- Bean Career modified with login: "+careerOfficer.getLogin());
//			printTrace("--- Bean Career modified with Surname: "+careerOfficer.getSurname());
//			printTrace("--- Bean Career modified with Firstname: "+careerOfficer.getFirstname());
//			printTrace("--- Bean Career modified with Email: "+careerOfficer.getEmail());
//			printTrace("--- Bean Career modified with password: "+careerOfficer.getPassword());
			
			
			
			
			
			// Test of Admin Bean
			beanType = "Admin";
			homeName = "MyAdmin_L";
			printTrace("-- Test of "+beanType+" bean");
			printTrace("--- Access to "+homeName+" : "+beanType+"Home");
			AdminHome adminHome = (AdminHome)initialContext.lookup(homeName);
			printTrace("--- Creation of a bean "+beanType);
			Admin admin = adminHome.create("Mamadou","Toure","admin","secret","mt@imagine.com");
			int admin1ID = admin.getId();
			printTrace("--- Bean "+beanType+" created with id: "+admin1ID);
			printTrace("--- Bean "+beanType+" created with id: "+admin.getId());
			printTrace("--- Bean "+beanType+" created with login: "+admin.getLogin());
			printTrace("--- Bean "+beanType+" created with Surname: "+admin.getSurname());
			printTrace("--- Bean "+beanType+" created with Firstname: "+admin.getFirstname());
			printTrace("--- Bean "+beanType+" created with Email: "+admin.getEmail());
			printTrace("--- Bean "+beanType+" created with password: "+admin.getPassword());
			printTrace("-----------------------------");
			
			
			DepartmentHome depHome = (DepartmentHome)initialContext.lookup("MyDepartment_L");
			Department dep = depHome.create("Computer Science");
			Department dep2 = depHome.create("Biology");
			
			SectorHome secHome = (SectorHome)initialContext.lookup("MySector_L");
			Sector sec = secHome.create("IT");
			Sector sec2 = secHome.create("Food");
			
			
//			beanType = "Degree";
//			homeName = "MyDegree_L";
//			printTrace("-- Test of "+beanType+" bean");
//			printTrace("--- Access to "+homeName+" : "+beanType+"Home");
//			DegreeHome degreeHome = (DegreeHome)initialContext.lookup(homeName);
//			printTrace("--- Creation of a bean "+beanType);
//			Degree degree = degreeHome.create("Degree","");
//			int admin1ID = admin.getId();
//			printTrace("--- Bean "+beanType+" created with id: "+admin1ID);
//			printTrace("--- Bean "+beanType+" created with id: "+admin.getId());
//			printTrace("--- Bean "+beanType+" created with login: "+admin.getLogin());
//			printTrace("--- Bean "+beanType+" created with Surname: "+admin.getSurname());
//			printTrace("--- Bean "+beanType+" created with Firstname: "+admin.getFirstname());
//			printTrace("--- Bean "+beanType+" created with Email: "+admin.getEmail());
//			printTrace("--- Bean "+beanType+" created with password: "+admin.getPassword());
//			printTrace("-----------------------------");
//			printTrace("--- Creation of a bean "+beanType);
//			admin = adminHome.create("Mathieu","Maret","root","linux4life","mm@imagine.com");
//			int admin2ID = admin.getId();
//			printTrace("--- Bean "+beanType+" created with id: "+admin2ID);
			
			 
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			// Test of Feedback Bean
//			beanType = "Feedback";
//			homeName = "MyFeedback_L";
//			printTrace("-- Test of "+beanType+" bean");
//			printTrace("--- Access to "+homeName+" : "+beanType+"Home");
//			FeedbackHome feedbackHome = (FeedbackHome)initialContext.lookup(homeName);
//			printTrace("--- Creation of a bean "+beanType);
//			Feedback feedback = feedbackHome.create("Sample Feedback","Some random infos",admin.getUser());
//			int feedbackID1 = feedback.getId();
//			printTrace("--- Bean "+beanType+" created with id: "+feedbackID1);
//			printTrace("--- Creation of a bean "+beanType);
//			Feedback feedback2 = feedbackHome.create("Another Sample Feedback","Some random infos (2)",admin.getUser());
//			int feedbackID2 = feedback2.getId();
//			printTrace("--- Bean "+beanType+" created with id: "+feedbackID2);
//			printTrace("-- Access to "+beanType+" bean with id"+feedbackID1);
//			feedback = feedbackHome.findByPrimaryKey(feedbackID1);
			
			// Test of Degree Bean
//			beanType = "Degree";
//			homeName = "MyDegree_L";
//			printTrace("-- Test of "+beanType+" bean");
//			printTrace("--- Access to "+homeName+" : "+beanType+"Home");
//			DegreeHome degreeHome = (DegreeHome)initialContext.lookup(homeName);
//			printTrace("--- Creation of a bean "+beanType);
//			Degree degree = degreeHome.create("Sample Degree","Infos about sample degree");
//			int degreeID1 = degree.getId();
//			printTrace("--- Bean "+beanType+" created with id: "+degreeID1);
//			printTrace("--- Creation of a bean "+beanType);
//			Degree degree2 = degreeHome.create("Another Sample Degree","Infos about another sample degree");
//			int degreeID2 = degree2.getId();
//			printTrace("--- Bean "+beanType+" created with id: "+feedbackID2);
//			printTrace("-- Access to "+beanType+" bean with id"+degreeID1);
//			degree = degreeHome.findByPrimaryKey(degreeID1);
//			
//			// Test of Student Bean
//			printTrace("-- Test of Student bean");
//			homeName = "MyStudent_L";
//			printTrace("--- Access to "+homeName+" : StudentHome");
//			StudentHome studentHome = (StudentHome)initialContext.lookup(homeName);
//			beanType = "Student";
//			printTrace("--- Creation of a bean "+beanType);
//			Student student1 = studentHome.create("Paul","Remignon","pr","milkyway","premignon@hotmail.com");
//			int student1ID = student1.getId();
//			printTrace("--- Bean Student created with id: "+student1ID);
//			printTrace("--- Creation of a bean Student");
//			Student student2;
//			student2 = studentHome.create("Mathieu","Maret","ptizizi","rulez","mmaret@hotmail.fr",degree2);
//			int student2ID = student2.getId();
//			printTrace("--- Bean Student created with id: "+student2ID);
//			Student student3;
//			student3 = studentHome.create("test1","test1","test2","test","test@test.fr",degree2);
//			int student3ID = student3.getId();
//			
//			Vector<Student> vecStudent = new Vector<Student>();
//			vecStudent.add(student1);
//			vecStudent.add(student2);
//			
//			//-- Test of Marker  Bean
//			printTrace("-- Test of Marker bean");
//			homeName = "MyMarker_L";
//			printTrace("--- Access to "+homeName+" : MarkerHome");
//			MarkerHome markerHome = (MarkerHome)initialContext.lookup(homeName);
//			beanType = "Marker";
//			printTrace("--- Creation of a bean "+beanType);
//			Marker marker = markerHome.create("marker1","marker1","marker1","marker1","premignon@hotmail.com");
//			int markerID = marker.getId();
//			printTrace("--- Bean Marker created with id: "+markerID);
//			
//			//-- Test of AcademicSup  Bean
//			beanType = "AcademicSup";
//			homeName = "MyAcademicSup_L";
//			printTrace("********** Test of "+beanType+" bean **********");
//			printTrace("--- Access to "+homeName+" : "+beanType+"Home");
//			AcademicSupHome academicSupHome = (AcademicSupHome)initialContext.lookup(homeName);
//			printTrace("--- Creation of a bean "+beanType);
//			AcademicSup academicSup = academicSupHome.create("John", "Doe", "jd", "pass", "jd@test.ie");
//			academicSup.setStudents(vecStudent);
//			printTrace("--- Bean AcademicSup created with id: "+academicSup.getId());
//			printTrace("--- Bean AcademicSup created with login: "+academicSup.getLogin());
//			printTrace("--- Bean AcademicSup created with Surname: "+academicSup.getSurname());
//			printTrace("--- Bean AcademicSup created with Firstname: "+academicSup.getFirstname());
//			printTrace("--- Bean AcademicSup created with Email: "+academicSup.getEmail());
//			printTrace("--- Bean AcademicSup created with password: "+academicSup.getPassword());
//			//printTrace("--- Bean AcademicSup created with company: "+academicSup.getCompany().getName());
//			printTrace("--- Bean AcademicSup created with student's collection: "+academicSup.getStudents().size());
//			/*while(academicSup.getStudents().iterator().hasNext())
//			{
//				Student st = (Student) academicSup.getStudents().iterator().next();
//				printTrace(" -" + st.getDescriptor().getName());
//			}*/
//			
//			printTrace("-----------------------------");
//			academicSup.setEmail("sjd@test.com");
//			academicSup.setFirstname("Jo");
//			academicSup.setLogin("jd1");
//			academicSup.setPassword("blabla1");
//			academicSup.setSurname("Doen");
//			academicSup.getStudents().remove(student1);
//			printTrace("--- Bean AcademicSup modified with id: "+academicSup.getId());
//			printTrace("--- Bean AcademicSup modified with login: "+academicSup.getLogin());
//			printTrace("--- Bean AcademicSup modified with Surname: "+academicSup.getSurname());
//			printTrace("--- Bean AcademicSup modified with Firstname: "+academicSup.getFirstname());
//			printTrace("--- Bean AcademicSup modified with Email: "+academicSup.getEmail());
//			printTrace("--- Bean AcademicSup modified with password: "+academicSup.getPassword());
//			//printTrace("--- Bean AcademicSup modified with company: "+academicSup.getCompany().getName());
//			printTrace("--- Bean AcademicSup modified with student's collection: " +academicSup.getStudents().size());
//			/*while(academicSup.getStudents().iterator().hasNext())
//			{
//				Student st = (Student) academicSup.getStudents().iterator().next();
//				printTrace(" -" + st.getDescriptor().getName());
//			}*/
//			
//			//-- Test of CV  Bean
//			beanType = "CV";
//			homeName = "MyCV_L";
//			printTrace("********** Test of "+beanType+" bean **********");
//			printTrace("--- Access to "+homeName+" : "+beanType+"Home");
//			CVHome cvHome = (CVHome)initialContext.lookup(homeName);
//			printTrace("--- Creation of a bean "+beanType);
//			CV cvOjbect = cvHome.create("CV_Paul1.pdf","./StudentCVs/CV_Paul1.pdf",false,student1);
//			printTrace("--- Bean CV created with id: "+cvOjbect.getId());
//			printTrace("--- Bean CV created with fileLocation: "+cvOjbect.getFilelocation());
//			printTrace("--- Bean CV created with authorisation: "+cvOjbect.getPublishAuth());
//			printTrace("--- Bean CV created with student owner: "+ cvOjbect.getStudent().getDescriptor().getLogin());//
//			printTrace("-----------------------------");
//			cvOjbect.setFilelocation("./CVStudent/CV_Paul1.pdf");
//			cvOjbect.setPublishAuth(true);
//			cvOjbect.setStudent(student3);
//			printTrace("--- Bean CV modified with id: "+cvOjbect.getId());
//			printTrace("--- Bean CV modified with fileLocation: "+cvOjbect.getFilelocation());
//			printTrace("--- Bean CV modified with authorisation: "+cvOjbect.getPublishAuth());
//			printTrace("--- Bean CV modified with student owner: " +cvOjbect.getStudent().getName() + " " +cvOjbect.getStudent().getFirstName());//
//			printTrace("--- Student : " +student3.getLogin() +"has : " + student3.getCVs().size());
//			
//			//-- Test of Query  Bean
//			beanType = "Query";
//			homeName = "MyQuery_L";
//			printTrace("********** Test of "+beanType+" bean **********");
//			printTrace("--- Access to "+homeName+" : "+beanType+"Home");
//			QueryHome queryHome = (QueryHome)initialContext.lookup(homeName);
//			printTrace("--- Creation of a bean "+beanType);
//			Query queryOjbect = queryHome.create("test message query",student2,academicSup);
//			printTrace("--- Bean Query created with id: "+queryOjbect.getId());
//			printTrace("--- Bean Query created with student: "+ queryOjbect.getStudent().getName());
//			printTrace("--- Bean Query created with message: "+queryOjbect.getMessage());
//			printTrace("--- Bean Query created with status: "+queryOjbect.getStatus());
//			printTrace("--- Bean Query created with academic supervisor: "+ queryOjbect.getAcademicSup().getSurname());//
//			printTrace("-----------------------------");
//			queryOjbect.setStatus(Query.REJECTED);
//			printTrace("--- Bean Query modified with id: "+queryOjbect.getId());
//			printTrace("--- Bean Query modified with student: "+ queryOjbect.getStudent().getName());
//			printTrace("--- Bean Query modified with message: "+queryOjbect.getMessage());
//			printTrace("--- Bean Query modified with status: "+queryOjbect.getStatus());
//			printTrace("--- Bean Query modified with academic supervisor: "+ queryOjbect.getAcademicSup().getSurname());
//			
//			//-- Test of PacementDiary  Bean
//			beanType = "PlacementDiary";
//			homeName = "MyPlacementDiary_L";
//			printTrace("********** Test of "+beanType+" bean **********");
//			printTrace("--- Access to "+homeName+" : "+beanType+"Home");
//			PlacementDiaryHome placementDiaryHome = (PlacementDiaryHome)initialContext.lookup(homeName);
//			printTrace("--- Creation of a bean "+beanType);
//			PlacementDiary placementDiaryOjbect = placementDiaryHome.create("02/04/08 : My first day", student2);
//			printTrace("--- Bean PlacementDiary created with id: "+placementDiaryOjbect.getId());
//			printTrace("--- Bean Query created with student: "+ placementDiaryOjbect.getStudent().getName());
//			printTrace("--- Bean PlacementDiary created with message: "+placementDiaryOjbect.getContent());
//			printTrace("-----------------------------");
//			placementDiaryOjbect.setContent("03/04/08 : Second day \n"+ placementDiaryOjbect.getContent());
//			printTrace("--- Bean PlacementDiary modified with id: "+placementDiaryOjbect.getId());
//			printTrace("--- Bean PlacementDiary modified with student: "+ placementDiaryOjbect.getStudent().getName());
//			printTrace("--- Bean PlacementDiary modified with message: "+placementDiaryOjbect.getContent());
//			
//			beanType = "Degree";
//			printTrace("--- Creation of a bean "+beanType);
//			homeName = "MyDegree_L";
//			printTrace("-- Test of "+beanType+" bean");
//			printTrace("--- Access to "+homeName+" : "+beanType+"Home");
//			DegreeHome degreeHome = (DegreeHome)initialContext.lookup(homeName);
//			printTrace("--- Creation of a bean "+beanType);
//			Degree degree = degreeHome.create("IT","Computer Science Degree");
//			printTrace("--- Bean "+beanType+" created with name: "+degree.getName());
//			printTrace("-----------------------------");
//			
//			printTrace("-- Test of Student bean");
//			homeName = "MyStudent_L";
//			printTrace("--- Access to "+homeName+" : StudentHome");
//			StudentHome studentHome = (StudentHome)initialContext.lookup(homeName);
//			beanType = "Student";
//			printTrace("--- Creation of a bean "+beanType);
//			Student student1 = studentHome.create("student1","STUDENT+","st1","st1st1","st1@wpms.com",degree);
//			int student1ID = student1.getId();
//			printTrace("--- Bean Student created with id: "+student1ID);
//			printTrace("-----------------------------");
//			
//			beanType = "Admin";
//			homeName = "MyAdmin_L";
//			printTrace("-- Test of "+beanType+" bean");
//			printTrace("--- Access to "+homeName+" : "+beanType+"Home");
//			AdminHome adminHome = (AdminHome)initialContext.lookup(homeName);
//			printTrace("--- Creation of a bean "+beanType);
//			Admin admin = adminHome.create("admin","admin","admin","admin","admin@wpms.com");
//			int admin1ID = admin.getId();
//			printTrace("--- Bean "+beanType+" created with id: "+admin1ID);
//			printTrace("-----------------------------");
			
			
			
		} catch (CreateException e) {
			e.printStackTrace(out);
		} catch (NamingException e) {
			e.printStackTrace(out);
		}// catch (FinderException e) {
//			e.printStackTrace(out);
//		}
			
		printPlain("</body>\n</html>");
		
	}
	
	public void printTrace(String s){
		out.println("<p>"+s+"</p>");
	}
	
	public void printPlain(String s){
		out.println(s);
	}
}





